/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package amanda_gabriel_isabella_lucivania_monica.View;

import amanda_gabriel_isabella_lucivania_monica.Model.Dao.DaoBaseImpl;
import java.util.List;
import java.util.Objects;
import java.util.Vector;
import javax.swing.DefaultListModel;

/**
 *
 * @author lucy
 */
public class RemoverAlunoIG extends javax.swing.JFrame {

    private DaoBaseImpl daoBaseImpl;

    /**
     * Creates new form RemoverAlunoIG
     *
     * @param daoBaseImpl
     */
    public RemoverAlunoIG(DaoBaseImpl daoBaseImpl) {
        initComponents();
        this.daoBaseImpl = daoBaseImpl;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jDSucesso = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jBSim = new javax.swing.JButton();
        jBNao = new javax.swing.JButton();
        jDCamposVazios = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        jBOK = new javax.swing.JButton();
        amanda_gabriel_isabella_lucivania_monicaPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("amanda_gabriel_isabella_lucivania_monicaPU").createEntityManager();
        alunoQuery = java.beans.Beans.isDesignTime() ? null : amanda_gabriel_isabella_lucivania_monicaPUEntityManager.createQuery("SELECT a FROM Aluno a");
        alunoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : alunoQuery.getResultList();
        jBCancelar = new javax.swing.JButton();
        jBRemover = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLAlunos = new javax.swing.JList();

        jDSucesso.setMinimumSize(new java.awt.Dimension(400, 175));

        jLabel1.setText("Aluno removido com sucesso!");

        jLabel2.setText("Deseja remover outro aluno?");

        jBSim.setText("Sim");
        jBSim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSimActionPerformed(evt);
            }
        });

        jBNao.setText("Não");
        jBNao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDSucessoLayout = new javax.swing.GroupLayout(jDSucesso.getContentPane());
        jDSucesso.getContentPane().setLayout(jDSucessoLayout);
        jDSucessoLayout.setHorizontalGroup(
            jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDSucessoLayout.createSequentialGroup()
                .addGroup(jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDSucessoLayout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addGroup(jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)))
                    .addGroup(jDSucessoLayout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(jBSim, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBNao, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jDSucessoLayout.setVerticalGroup(
            jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDSucessoLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSim)
                    .addComponent(jBNao))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        jDCamposVazios.setMinimumSize(new java.awt.Dimension(400, 175));

        jLabel3.setText("Todos os campos devem ser preenchidos!");

        jBOK.setText("OK");
        jBOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDCamposVaziosLayout = new javax.swing.GroupLayout(jDCamposVazios.getContentPane());
        jDCamposVazios.getContentPane().setLayout(jDCamposVaziosLayout);
        jDCamposVaziosLayout.setHorizontalGroup(
            jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                .addGroup(jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jLabel3))
                    .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jBOK, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jDCamposVaziosLayout.setVerticalGroup(
            jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jBOK)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Remover aluno");

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        jBRemover.setText("Remover");
        jBRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRemoverActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Por favor, seleciona o aluno o qual deseja remover:"));
        jPanel1.setToolTipText("");

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, alunoList, jLAlunos);
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(jLAlunos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jBRemover)
                        .addGap(18, 18, 18)
                        .addComponent(jBCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBCancelar)
                    .addComponent(jBRemover))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jBCancelarActionPerformed
    
    private void jBRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRemoverActionPerformed
        //EntidadeDisciplina disciplina = (EntidadeDisciplina) jLDisciplinas.getSelectedValue();
        int index = jLAlunos.getSelectedIndex();
        
        if (index == -1) {
            setVisible(false);
            jDCamposVazios.setVisible(true);
        } else {            
            Object objeto = jLAlunos.getModel().getElementAt(index);
            daoBaseImpl.remove(objeto);
            jDSucesso.setVisible(true);
        }
        
        
        jLAlunos.setModel(new DefaultListModel());
        
        List<Objects> objetos = daoBaseImpl.executarQuery("SELECT p FROM Aluno p",null,null);
        Vector objetos2 = new Vector();
        for(Object objeto:objetos){
            objetos2.addElement(objeto);
        }
        jLAlunos.setListData(objetos2);
        jLAlunos.setVisible(true);
        
        
    }//GEN-LAST:event_jBRemoverActionPerformed
    
    private void jBSimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSimActionPerformed
        jDSucesso.dispose();
        setVisible(true);
    }//GEN-LAST:event_jBSimActionPerformed
    
    private void jBNaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNaoActionPerformed
        jDSucesso.dispose();
        setVisible(false);
    }//GEN-LAST:event_jBNaoActionPerformed
    
    private void jBOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOKActionPerformed
        setVisible(true);
        jDCamposVazios.dispose();
    }//GEN-LAST:event_jBOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Aluno> alunoList;
    private javax.persistence.Query alunoQuery;
    private javax.persistence.EntityManager amanda_gabriel_isabella_lucivania_monicaPUEntityManager;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBNao;
    private javax.swing.JButton jBOK;
    private javax.swing.JButton jBRemover;
    private javax.swing.JButton jBSim;
    private javax.swing.JDialog jDCamposVazios;
    private javax.swing.JDialog jDSucesso;
    private javax.swing.JList jLAlunos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
